<?php
declare (strict_types = 1);

namespace app\middleware\admin;

use app\repositories\contracts\AdminLogInterface;

class AdminLog
{
    protected $adminLog;
    public function __construct(AdminLogInterface $adminLog)
    {
        $this->adminLog = $adminLog;
    }

    /***
     * @param $request
     * @param \Closure $next
     * @return mixed
     * @Author Vee7
     * @Date 2021/12/22
     * @Time 14:25
     * @Description 日志记录中间件
     */
    public function handle($request, \Closure $next)
    {
        $rootData = $request->rootData;
        $user_id = $rootData['id'];    //管理员ID
        $method = $request->method();  //请求方法[GET POST]
        $url = $request->url(true);    //请求路径URL
        $ip = $request->ip();          //IP地址
        $input = $request->param();    //请求参数
        //不记录GET，否则会记录所有浏览页面
        if(!$request->isGet()){
            self::insertLog($user_id,$input,$url,$method,$ip);
        }
        return $next($request);
    }

    public function insertLog($admin_id,$input,$path,$method,$ip)
    {
        $result = $this->adminLog->addAdminLogList($admin_id,$input,$path,$method,$ip);
        if($result === false){
            return redirect('/admin/root_group/NoPromise/errorMsg/非法操作');
        }
    }
}
